local class = reload("class")
local super = reload("super.basic")
local clickhouse = reload("clickHouseLC")
local logdebug = reload("single.logdebug")
local this = class(super)


function this:ctor(info)
    super.ctor(self)
    self._info = info
    self._index = 0
    self._auth = 0
end

function this:launch()
    super.launch(self)
    local list = {}
    self._list = list
    self:async(self.link, { list = list })
    return self:hand()
end

function this:link(arg)
    local info = self._info
    local last = 1
    while info.list[last] do
        local name = info.list[last]
        local obj = clickhouse.new(name, info)
        obj:launch()({
            auth = function ()
                logdebug("auth ", name)
                self:coback(arg)
            end,
        })
        self:await()

        table.insert(arg.list, obj)
        last = last + 1
    end
    ---总体验证完成
    self._hand.auth()
end

function this:hand()
    return function (hand)
        self._hand = hand
    end
end

function this:fetch()
    local index = self._index
    local list = self._list
    self._index = index + 1
    local idx = (index % #list) + 1
    return list[idx]
end

function this:first()
    local aobj = self._list[1]
    return aobj
end

return this
